﻿using System;
using NUnit.Framework;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace SingletonService
{
  public class Program
  {
    [Test]
    public static void Main()
    {
      // Loading domain configuration section with name "Default". 
      var domainConfiguration = DomainConfiguration.Load("Default");
      var domain = Domain.Build(domainConfiguration);

      // Accessing domain service
      var service1 = domain.Services.Get<IMyDomainService>();
      Assert.IsNotNull(service1);
      Console.WriteLine("Successfully obtained IMyDomainService through Domain.Services");

      var service2 = domain.Services.Get<IMyDomainService>();
      Assert.AreSame(service1, service2);
      Console.WriteLine("service1 and service2 are same instances of IMyDomainService");

      // Accessing domain service through session
      using (var session = domain.OpenSession()) {
        service1 = session.Services.Get<IMyDomainService>();
        Assert.IsNotNull(service1);
        Console.WriteLine("Successfully obtained IMyDomainService through Session.Services");

        service2 = session.Services.Get<IMyDomainService>();
        Assert.AreSame(service1, service2);
        Console.WriteLine("service1 and service2 are same instances of IMyDomainService");
      }
    }
  }
}
